<template>
  <div class="container" >
    <el-col :span="24" style="margin-top: 30px;text-align: left;border: 1px solid #ccc">
      <!--      <el-col :span="24" style="margin-top: 30px;margin-bottom: 30px">
              <h3 style="font-size: 2.0vw">居民信息填写</h3>
            </el-col>-->
      <el-col :span="24">
        <el-form ref="form" :model="addParam" label-width="120px" style="margin-top: 30px">

          <el-form-item label="头像">
            <el-upload
              class="upload-demo"
              ref="businessLicense"
              action
              :auto-upload="false"
              :on-change="uploadSectionFile"
              :file-list="fileList"
              list-type="picture">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>

          <el-form-item label="姓名">
            <el-input v-model="addParam.name" style="width: 20%"></el-input>
          </el-form-item>

          <el-form-item label="用户名">
            <el-input v-model="addParam.username" style="width: 20%"></el-input>
          </el-form-item>

          <el-form-item label="密码">
            <el-input type="password" v-model="addParam.password" style="width: 20%"></el-input>
          </el-form-item>


          <el-form-item label="手机号码">
            <el-input v-model="addParam.phone" style="width: 20%"></el-input>
          </el-form-item>

          <el-form-item label="地址">
            <el-input v-model="addParam.address" style="width: 20%"></el-input>
          </el-form-item>

          <el-form-item label="生日">
            <el-date-picker
              v-model="addParam.birthday"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>

       <!--   <el-form-item label="角色">
            <el-radio-group v-model="addParam.role_id">
              <el-radio :label="1" style="font-weight: bold;font-size: 1vw">用户</el-radio>
            </el-radio-group>
          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
            <el-button @click="returnList">取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-col>
  </div>
</template>

<script>
  export default {
    name: "list",
    data() {
      return {
        addParam: {
          id:'',
          name: '',
          username: '',
          password: '',
          phone: '',
          address: '',
          birthday: '',
          avatar: '',
          role_id: 1,
        },
        fileList: []
      }
    },
    created() {

    },
    mounted() {

    },
    methods: {
      returnList(){
        this.$router.push('/home/OwnerList')
      },
      uploadSectionFile(file) {
        const typeArr = ['image/png', 'image/gif', 'image/jpeg', 'image/jpg'];
        const isJPG = typeArr.indexOf(file.raw.type) !== -1;
        const isLt3M = file.size / 1024 / 1024 < 3;
        if (!isJPG) {
          this.$message.error('只能是图片!');
          this.$refs.upload.clearFiles();
          this.files = null;
          return;
        }
        if (!isLt3M) {
          this.$message.error('上传图片大小不能超过 3MB!');
          this.$refs.upload.clearFiles();
          this.files = null;
          return;
        }
        this.files = file.raw;
        // FormData 对象
        let formData = new FormData();
        // 文件对象
        formData.append('file', this.files);
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          },
          methods: 'post'
        }
        this.service.post("/tbowner/fileUpload", formData, config).then(res => {
          this.addParam.avatar = res.data.filename
        })
      },
      onSubmit() {
        if(this.checkParam() != null){
          this.$message.error(this.checkParam())
          return false;
        }
        this.service.post('/tbowner/add'
          , {data: this.addParam})
          .then(result => {
            let data = result.data;
            if (data.code == 500) {
              this.$message.error("请求失败：" + data.data)
            }
            if (data.data > 0) {
              this.$message.success('增加成功')
              this.initAddParam();
              this.$router.push('/login')
            } else {
              this.$message.error('增加失败')
            }
          })
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      initAddParam(){
        for (let item in this.addParam){
           this.addParam[item] = '';
        }
      },
      checkParam(){
        if(this.addParam.name.length < 1){
          return '姓名不能为空！';
        }
        if(this.addParam.username.length < 1){
          return '用户名不能为空！';
        }
        if(this.addParam.password.length < 1){
          return '密码不能为空！';
        }
        return null;
      },
      handlePreview(file) {
        console.log(file);
      }
    }
  }
</script>

<style scoped>

</style>
